package com.omkarpatil.learningjpa2.mapping.basic.singleclass;

import java.util.Calendar;
import java.util.GregorianCalendar;

import javax.persistence.EntityManager;

import com.omkarpatil.learningjpa2.util.EntityManagerFactoryFactory;

public class TestDriver {

	public static void main(String[] args) {
		/* Creating a person */
		EntityManager em1 = EntityManagerFactoryFactory.getEntityManagerFactory().createEntityManager();
		em1.getTransaction().begin();
		
		Person person = new Person();
		person.setFirstName("Rewa");
		person.setLastName("Patil");
		person.setGender(Gender.FEMALE);
		
		Calendar cal = GregorianCalendar.getInstance();
		cal.set(2006, Calendar.AUGUST, 2);
		
		person.setBirthDate(cal.getTime());
		
		em1.persist(person);
		
		em1.getTransaction().commit();
		em1.close();
		
		int personId = person.getId();
		System.out.println("##### Added person with id = " + personId);
		
		/* Finding a person added above */
		EntityManager em2 = EntityManagerFactoryFactory.getEntityManagerFactory().createEntityManager();
		em2.getTransaction().begin();
		System.out.println("##### Finding person with id = " + personId);
		Person person2 = em2.find(Person.class, personId);
		System.out.println("##### Person Details :\n" + person2);
		
		System.out.println("##### Deleting person with id = " + personId);
		//Deleting person
		em2.remove(person2);
		
		//connfirm delete
		Person person3 = em2.find(Person.class, personId);
		System.out.println("##### Is person still there? " + (person3 == null ? "No" : "Yes"));
		
		em2.getTransaction().commit();
		em2.close();
		
		System.out.println(Gender.MALE.ordinal());
	}
}
